package com.coinland.study.window;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.coinland.study.R;
import com.coinland.study.bean.DocumentBean;
import com.coinland.study.widgt.CircleImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述:
 * 创建人:  shenZ
 * 创建时间: 2020/4/15
 * 联系方式: 252079249@qq.com
 * 修改时间: 2020/4/15
 */
public class DocumentAdapter extends BaseAdapter{
    Context mContext;
    int mDirection;
    List<DocumentBean> mDatas;
    private LayoutInflater mInflater;
    // 缓存类
    private ViewHolder holder;

    private IDeleteItem iDeleteItem;


    public DocumentAdapter(Context mContext, List<DocumentBean> mDatas,int mDirection){

        this.mContext = mContext;
        this.mDatas = mDatas;
        this.mDirection = mDirection;
        this.mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return null == mDatas ? 0 : mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    /**
     * 在原始数据上添加新数据 <功能详细描述>
     *
     * @param data
     * @see
     */
    public void addData(List<DocumentBean> data) {
        if (mDatas == null) {
            mDatas = new ArrayList<>();
        }
        mDatas.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            holder = new ViewHolder();
            // 右上  添加ListView在 mCustomView 右下
            if(mDirection == WindowUtil.DIRECTION_RIGHT_TOP ||  mDirection ==WindowUtil.DIRECTION_RIGHT_BOTTOM){
                convertView = mInflater.inflate(R.layout.layout_more_popwin_item_r, null);
            }else if(mDirection == WindowUtil.DIRECTION_LEFT_BOTTOM || mDirection == WindowUtil.DIRECTION_LEFT_TOP){
                convertView = mInflater.inflate(R.layout.layout_more_popwin_item_l, null);
            }
            holder.imageView = (CircleImageView)convertView.findViewById(R.id.iv_icon);
            holder.btDel = (ImageView) convertView.findViewById(R.id.iv_del);
            holder.textTitle = (TextView) convertView.findViewById(R.id.tv_docName);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        DocumentBean docInfo = mDatas.get(position);
        holder.imageView.setImageResource(docInfo.getThumb());
        holder.btDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iDeleteItem.deleteAnim(position);
            }
        });
        return convertView;
    }

    private class ViewHolder {
        CircleImageView imageView;
        TextView textTitle;
        ImageView btDel;

    }

    public void setIDeleteItem(IDeleteItem iDeleteItem){
        this.iDeleteItem = iDeleteItem;
    }

    public interface IDeleteItem{
        // 删除动画
        void deleteAnim(int position);
    }

}
